package com.netty.nio;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Lee
 * @date 2020/7/28 0028
 */
public class NioFileChannel02 {
    public static void main(String[] args) throws Exception {
        File file = new File("E:\\fileout.txt");
        //创建文件的输入流
        FileInputStream fileInputStream = new FileInputStream(file);
        //获取fileChannel
        FileChannel fileChannel = fileInputStream.getChannel();
        //创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
        //将数据从Channel读到缓冲区byteBuffer中
        fileChannel.read(byteBuffer);
        //将byteBuffer的字节转为String
        //byteBuffer.array()是将byteBuffer里面的hb,也就是字节数组形式
        System.out.println(new String(byteBuffer.array()));
        fileInputStream.close();
    }
}
